<?php

/**
 * This is the model class for table "invoices".
 *
 * The followings are the available columns in table 'invoices':
 * @property integer $invoice_id
 * @property integer $status
 * @property string $payment_method
 * @property string $payment_code
 * @property string $token
 * @property double $total_purchased
 * @property int $total_order
 * @property integer $created
 * @property integer $updated
 * @property string $comment
 * @property string $params
 * @property integer $created_by
 * @property integer $updated_by
 * @property integer $is_delivery
 * @property string $payment_info
 * @property string $refund_info
 */
class Invoice {

    const STATUS_PENDING = 1;
    const STATUS_APPROVE = 2;
    const STATUS_CANCEL = 3;
    const STATUS_DENY = 4;
    const DELIVERY_NOT = 0;
    const DELIVERIED = 1;

    //define model
    const MODEL_USER = 1;
            
    public $tableName = 'travel_invoices';
    /**
     * 
     * @param array $aInvoice contains all information of invoice
     */
    public function add($aInvoice){
	global $wpdb;
	$sql = 'INSERT INTO `'.$this->tableName.'`(`status`, `payment_method`, `payment_code`, `total_order`
	    , `total_purchased`, `create_time`, `item_id`) 
	    VALUES('.self::STATUS_PENDING.', "paypal", "'.$aInvoice['payment_code'].'", '.$aInvoice['total_order'].', 
		'.$aInvoice['total_purchased'].', '.time().', '.$aInvoice['item_id'].')';		
	$wpdb->query($sql);	
    }
    /**
     * 
     * @param array $aInvoice contains all information of invoice
     */
    public function update($aInvoice, $token){
	global $wpdb;
	$sql = "UPDATE `".$this->tableName."` SET `status`=".self::STATUS_APPROVE.", 
	    `payment_info`='".$aInvoice['payment_info']."' WHERE `payment_code`='".$token."'";
	
	$wpdb->query($sql);
    }
    /**
     * get invoice based on token
     * @global type $wpdb
     * @param type $token
     * @return type
     */
    public function getByToken($token){
	global $wpdb;
	$sql = 'SELECT * FROM '.$this->tableName.' WHERE `payment_code`="'.$token.'"';
	$data = $wpdb->get_results($sql);
	return $data;
    }

}

